//
//  SystermAccessTool.m
//  Pods
//
//  Created by 正合适 on 2017/9/21.
//
//

#import "SystermAccessTool.h"
#import <AVFoundation/AVCaptureDevice.h>
#import <AVFoundation/AVMediaFormat.h>
#import <AssetsLibrary/AssetsLibrary.h>
#import <CoreLocation/CoreLocation.h>
#import <UserNotifications/UserNotifications.h>
#import <CoreTelephony/CTCellularData.h>

@implementation SystermAccessTool
#pragma mark - 访问相机的权限
+ (BOOL)accessToCamera {
    AVAuthorizationStatus authStatus =  [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    if (authStatus == AVAuthorizationStatusRestricted || authStatus ==AVAuthorizationStatusDenied) {
        // 没有访问照片的权限
        return NO;
    } else {
        return YES;
    }
}

#pragma mark - 访问相册的权限
+ (BOOL)accessToPhoto {
    ALAuthorizationStatus status = [ALAssetsLibrary authorizationStatus];
    if (status == ALAuthorizationStatusRestricted || status == ALAuthorizationStatusDenied) {
        // 没有访问照片的权限
        return NO;
    } else {
        return YES;
    }
}

#pragma mark - 定位权限
+ (BOOL)accessToLocation {
    BOOL isOPen = NO;
    if ([CLLocationManager locationServicesEnabled] && [CLLocationManager authorizationStatus] != kCLAuthorizationStatusDenied) {
        isOPen = YES;
    }
    return isOPen;
}

#pragma mark - 消息推送权限
+ (BOOL)accessToNotification {
    BOOL isOpen = NO;
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_8_0
    UIUserNotificationSettings *setting = [[UIApplication sharedApplication] currentUserNotificationSettings];
    if (setting.types != UIUserNotificationTypeNone) {
        isOpen = YES;
    }
#else
    UIRemoteNotificationType type = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
    if (type != UIRemoteNotificationTypeNone) {
        isOpen = YES;
    }
#endif
    return isOpen;
}

#pragma mark - 是否开启蜂窝网络
+ (void)accessToNetwork:(void(^)(NSInteger result))returnBolck {
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_9_0
    CTCellularData *cellularData = [[CTCellularData alloc] init];
    cellularData.cellularDataRestrictionDidUpdateNotifier = ^(CTCellularDataRestrictedState state){
        if (state == kCTCellularDataRestrictedStateUnknown || state == kCTCellularDataNotRestricted) {
            if (returnBolck) {
                returnBolck(0);
            }
        } else {
            if (returnBolck) {
                returnBolck(1);
            }
        }
    };
    CTCellularDataRestrictedState state = cellularData.restrictedState;
    if (state == kCTCellularDataRestrictedStateUnknown || state == kCTCellularDataNotRestricted) {
        if (returnBolck) {
            returnBolck(0);
        }
    } else {
        if (returnBolck) {
            returnBolck(1);
        }
    }
#endif
}

+ (void)accessToCameraWithAlertStr:(NSString *)info {
    if (![SystermAccessTool accessToCamera]) {
        if (info.length == 0) {
            info = @"由于您的手机相机权限未开启，请前往系统设置开启";
        }
        [SystermAccessTool alertWithInfo:info];
    }
}

+ (void)accessToPhotoWithAlertStr:(NSString *)info {
    if (![SystermAccessTool accessToPhoto]) {
        if (info.length == 0) {
            info = @"由于您的手机相册权限未开启，请前往系统设置开启";
        }
        [SystermAccessTool alertWithInfo:info];
    }
}

+ (void)accessToLocationWithAlertStr:(NSString *)info {
    if (![SystermAccessTool accessToLocation]) {
        if (info.length == 0) {
            info = @"由于您的手机定位权限未开启，请前往系统设置开启";
        }
        [SystermAccessTool alertWithInfo:info];
    }
}

+ (void)accessToNotificationWithAlertStr:(NSString *)info {
    if (![SystermAccessTool accessToLocation]) {
        if (info.length == 0) {
            info = @"由于您的手机推送权限未开启，请前往系统设置开启";
        }
        [SystermAccessTool alertWithInfo:info];
    }
}

+ (void)accessToNetworkWithAlertStr:(NSString *)info {
    __block NSString *infos = info;
    [SystermAccessTool accessToNetwork:^(NSInteger result) {
        if (!result) {
            if (infos.length == 0) {
                infos = @"由于您的手机推送权限未开启，请前往系统设置开启";
            }
            [SystermAccessTool alertWithInfo:infos];
        }
    }];
}


+ (void)alertWithInfo:(NSString *)info {
    UIAlertController *alertView = [UIAlertController alertControllerWithTitle:@"温馨提示" message:info preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"去设置" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        // 跳转到系统设置界面
        NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
        if ([[UIApplication sharedApplication] canOpenURL:url]) {
            [[UIApplication sharedApplication] openURL:url];
        }
    }];
    [alertView addAction:cancelAction];
    [alertView addAction:confirmAction];
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alertView animated:YES completion:nil];
}


@end
